package com.sluggard.security.manage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sluggard.mp.model.req.IdsReq;
import com.sluggard.security.manage.mapper.RoleMapper;
import com.sluggard.security.manage.model.entity.MenuEntity;
import com.sluggard.security.manage.model.entity.RoleEntity;
import com.sluggard.security.manage.model.res.Option;
import com.sluggard.security.manage.service.IMenuService;
import com.sluggard.security.manage.service.IRoleMenuService;
import com.sluggard.security.manage.service.IRoleService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.List;

/**
 * <p>
 * 角色xinxi 服务实现类
 * </p>
 *
 * @author lizheng
 * @since 2024-04-28
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, RoleEntity> implements IRoleService {

    @Resource
    private IRoleMenuService roleMenuService;

    @Resource
    private IMenuService menuService;

    @Override
    public List<MenuEntity> getMenuNodeByRoleId(Long id) {
        List<Long> menuIds = roleMenuService.selectByRoleId(id);
        if (CollectionUtils.isEmpty(menuIds)) {
            return Collections.emptyList();
        }
        QueryWrapper<MenuEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("id", menuIds);
        return menuService.tree(queryWrapper);

    }

    @Override
    public List<Option> roleList(String name) {
        return baseMapper.selectByName(name);
    }

    @Override
    public void onDelete(IdsReq idsReq) {
        for (Long id : idsReq.getIds()) {
            roleMenuService.deleteByRoleId(id);
        }
    }
}
